package logic

import (
	"context"
	table "pd/server/app/pd/data/user"
	form "pd/server/app/pd/form/user"
	model "pd/server/app/pd/model/user"
	"pd/server/plugin/auth"
	"pd/server/plugin/logger"
	"time"
)

var userTable table.UserTable

type UserLogic struct {
}

func GetUserMerchantId(ctx context.Context, userId int64) (merchantId int64, err error) {
	return table.GetUserMerchantId(ctx, userId)
}

func (this *UserLogic) Login(ctx context.Context, frm *form.LoginForm) (resp map[string]interface{}, err error) {
	user, err := userTable.Login(ctx, frm)
	if err != nil {
		return
	}
	resp = make(map[string]interface{})
	resp["token"], err = auth.GenerateTokenUserId(user.Id)
	if err != nil {
		logger.Error(err)
		return
	}
	resp["user_id"] = user.Id
	resp["userName"] = user.UserName
	resp["realName"] = user.RealName
	if resp["menuTreeList"], err = userTable.GetUserPowerList(ctx, user.Id); err != nil {
		logger.Error(err)
		return
	}
	return
}

func (this *UserLogic) GetUserResources(ctx context.Context, userId int64) (menuList []*model.PowerNode, err error) {
	return userTable.GetUserPowerList(ctx, userId)
}

func (this *UserLogic) SetPower(ctx context.Context, frm *model.PowerNode) (err error) {
	return userTable.SetPower(ctx, frm)
}

func (this *UserLogic) EmployeeList(ctx context.Context, frm *form.EmployeeListForm) (userList []*model.UserModel, total int64, err error) {
	return userTable.EmployeeList(ctx, frm)
}

func (this *UserLogic) AddEmployee(ctx context.Context, frm *form.AddEmployeeForm) (err error) {
	frm.CreateTime = time.Now()
	frm.LastUpdateTime = time.Now()

	return userTable.AddEmployee(ctx, frm)
}

func (this *UserLogic) ModifyPassword(ctx context.Context, frm *form.ModifyPasswordForm) (err error) {
	return userTable.ModifyPassword(ctx, frm)
}

func (this *UserLogic) UpdateEmployee(ctx context.Context, frm *form.AddEmployeeForm) (err error) {
	return userTable.UpdateEmployee(ctx, frm)
}

func (this *UserLogic) RemoveEmployee(ctx context.Context, frm *form.AddEmployeeForm) (err error) {
	return userTable.RemoveEmployee(ctx, frm)
}
